#ifndef __temp_timer_c__
#define __temp_timer_c__

#include <zneo.h>

#include "temp_timer.h"
#include "clock.h"

// Timer enable and disable
#define TIMER_DISABLE ~0x80
#define TIMER_ENABLE   0x80

// Timer modes
#define TIMER_MODE_ONESHOT        0x00
#define TIMER_MODE_CONTINUOUS     0x01
#define TIMER_MODE_COUNTER        0x02
#define TIMER_MODE_PWM            0x03
#define TIMER_MODE_CAPTURE        0x04
#define TIMER_MODE_COMPARE        0x05
#define TIMER_MODE_GATED          0x06
#define TIMER_MODE_CAPTURECOMPARE 0x07

// Timer prescale values
#define TIMER_PRESCALE_1   0x00
#define TIMER_PRESCALE_2   0x08
#define TIMER_PRESCALE_4   0x10
#define TIMER_PRESCALE_5   0x18
#define TIMER_PRESCALE_16  0x20
#define TIMER_PRESCALE_32  0x28
#define TIMER_PRESCALE_64  0x30
#define TIMER_PRESCALE_128 0x38

// Timer output pin polarity
#define TIMER_TPOL_1 0x40
#define TIMER_TPOL_0 0x00

#define T0PRESCALE 1

void init_timer0(void) {
	T0CTL1 &= TIMER_DISABLE;

	T0CTL1 = TIMER_MODE_CONTINUOUS | TIMER_PRESCALE_1 | TIMER_TPOL_0;
	T0CTL0 = 0x00;

	T0HL = 0x0001;
	T0R  = SYS_CLK_FREQ / (T0PRESCALE * 55000);

	IRQ0ENH |= 0x20;
	IRQ0ENL |= 0x20;

	T0CTL1 |= TIMER_ENABLE;
}

#define T1PRESCALE 128

void init_timer1(void) {
	T1CTL1 &= TIMER_DISABLE;

	T1CTL1 = TIMER_MODE_CONTINUOUS | TIMER_PRESCALE_128 | TIMER_TPOL_0;
	T1CTL0 = 0x00;

	T1HL = 0x0001;
	//T1R  = SYS_CLK_FREQ / (T1PRESCALE * 1);
	T1R = 0x8000;

	IRQ0ENH |= 0x40;
	IRQ0ENL |= 0x40;

	T1CTL1 |= TIMER_ENABLE;
}


#endif